<?php
/**
 * Created by PhpStorm.
 * User: 10037
 * Date: 2022/5/28
 * Time: 18:56
 */

namespace app\admin\controller;


use app\admin\model\City;
use app\admin\model\User;
use think\facade\Request;

class UserInfo
{
    public static $cityId;

    public function addUsers(){
        $users=Request::post();

        if(empty($users['userImg'])){
            $users['userImg']='/img/admin.png';
        }

        $users['addUserTime']=date('Y-m-d',time());
        $users['password']='123456';
        $mode=new User();
        $text= mb_substr($users['address'],0,2);
        $result=$mode->addNewUser($users);

        if($result){
            $modeT=new City();
            $modeT->incProvinCount($text);
            return json(
                [
                    'message'=>'添加成功',
                    'status'=>200,
                    'zbc'=>$text
                ]
            );
        }

        return json(
            [
                'message'=>'添加失败',
                'status'=>300
            ]
        );
    }

    public function selectAddress(){
        $text=Request::post('text');

        $mode=new City();

        $result= $mode->selectLikeAddress($text);

        $arr=[];
        foreach ($result as $value){
           $result=$this->selectPid($value).$value['cityname'];
           $value['cityname']=$result;
           array_push($arr,$value);
        }

        return $arr;
    }

    public function selectPid($value){
        if($value['pid']===1){
            return ;
        }else{
            $mode=new City();
            $result= $mode->find($value['pid']);
            return $this->selectPid($result).$result['cityname'];
        }
    }

    public function selectPageUser(){
        $page=Request::post('page');

        $mode=new User();
        $result=$mode->selectUserAll($page);
        $userNum=$mode->selectUserCount();

        return json(
            [
                "userList"=>$result,
                "userCount"=>$userNum
            ]
        );
    }

    public function selectIdUser(){
        $id=Request::post('id');

        $mode=new User();

        $result= $mode->selectIdUser($id);

        if(empty($result)){
            return ;
        }

        return $result;
    }

    public function updateUserInfo(){
        $data= Request::post();
        $mode=new User();
        $result= $mode->selectIdUser($data['id']);

        $result['username']=$data['username'];

        $result['address']=$data['address'];

        if(!empty($data['userImg'])) {
            $result['userImg'] = $data['userImg'];
        }
        $result['password']=$data['password'];

        $isUpdate=$result->save();

        if($isUpdate){
            return json(
                [
                    "message"=>"修改成功",
                    "status"=>200
                ]
            );
        }

        return json(
            [
                "message"=>"修改失败",
                "status"=>400
            ]
        );
    }

    public function deleteUser(){
        $id=Request::post('id');

        $mode=new User();
        $result=$mode->deleteUser($id);

        if($result){
            return json(
                ['status'=>200]
            );
        }
        return json(
            ['status'=>400]
        );

    }

    public function selectLikeUser(){
        $text= Request::post('text');
        $mode=new User();
        return $mode->likeSelect($text);
    }

    public function updateAdmin(){
        $id=Request::post('id');
        $mode=new User();
        $admin= $mode->find($id);
        $admin['username']=Request::post('username');
        if(!empty(Request::post('userImg'))) {
            $admin['userImg'] = Request::post('userImg');
        }
        $admin['address']=Request::post('address');
        $admin['textInfo']=Request::post('textInfo');
        $admin->save();
    }

    public function selectTimeUserCount(){
        $today=Request::post('today');
        $Yesterday=Request::post('Yesterday');
        $Daybeforeyesterday=Request::post('Daybeforeyesterday');

        $mode=new User();
        $todayCount=$mode->selectTimeUserCount( $today);
        $YesterdayCount=$mode->selectTimeUserCount( $Yesterday);
        $DaybeforeyesterdayCount=$mode->selectTimeUserCount($Daybeforeyesterday);

        return json([
            'todayCount'=>$todayCount,
            'YesterdayCount'=>$YesterdayCount,
            'DaybeforeyesterdayCount'=>$DaybeforeyesterdayCount
        ]);
    }
}